#!/bin/bash
# leebor
# 2017-10-31
# install php7
# v1.0

DOWN_DIR='/data/downloads'
INSTALL_DIR='/opt/php7'
EXT_DIR='/tmp/php-7.1.11/ext/'
UNZIP_DIR='/tmp/php-7.1.11'



if [ ! -f /data/downloads ];then
	mkdir -p /data/downloads
fi

yum install -y libxml2 libxml2-devel curl-devel


# install php-fpm mbstring.so bcmath --enable-mysqlnd
function Install_php() {
	wget -P $DOWN_DIR http://cn2.php.net/distributions/php-7.1.11.tar.gz
	if [ $? -eq 0 ];then
		cd $DOWN_DIR
		tar -zxf $DOWN_DIR/php-7.1.11.tar.gz -C /tmp/
	fi
	if [ $? -eq 0 ];then
		cd /tmp/php-7.1.11
		./configure --prefix=$INSTALL_DIR --enable-mbstring  --enable-fpm --enable-bcmath --enable-mysqlnd --with-mysql --with-pdo-mysql --enable-zip --with-zlib --with-config-file-path=$INSTALL_DIR/etc/
		if [ $? -eq 0 ];then
			make
			make install
		fi
		cp php.ini-production $INSTALL_DIR/etc/php.ini
		cp /opt/php7/etc/php-fpm.conf.default /opt/php7/etc/php-fpm.conf
		cp $INSTALL_DIR/etc/php-fpm.d/www.conf.default $INSTALL_DIR/etc/php-fpm.d/www.conf
		echo "[extensions]" >> $INSTALL_DIR/etc/php.ini
	fi
}

# install raphf.so
function Install_raphf() {
	wget -P $DOWN_DIR http://pecl.php.net/get/raphf-2.0.0.tgz
	tar -zxf $DOWN_DIR/raphf-2.0.0.tgz -C /tmp/
	if [ -d /tmp/raphf-2.0.0 ];then
		cd /tmp/raphf-2.0.0
		$INSTALL_DIR/bin/phpize 
		./configure --with-php-config=$INSTALL_DIR/bin/php-config
		if [ $? -eq 0 ];then
			make && make install
			echo "extension=raphf.so" >> $INSTALL_DIR/etc/php.ini
		fi
	rm -rf /tmp/raphf-2.0.0
	fi
}

# install raphf.so
function Install_propro() {
	wget -P $DOWN_DIR http://pecl.php.net/get/propro-2.0.1.tgz
	tar -zxf $DOWN_DIR/propro-2.0.1.tgz -C /tmp	
	if [ -d /tmp/propro-2.0.1 ];then
		cd /tmp/propro-2.0.1
		$INSTALL_DIR/bin/phpize
		./configure --with-php-config=$INSTALL_DIR/bin/php-config
		if [ $? -eq 0 ];then
			make && make install
			echo "extension=propro.so" >> $INSTALL_DIR/etc/php.ini
		fi
	rm -rf /tmp/propro-2.0.1
	fi
}

# install http.so
function Install_http() {
	wget -P $DOWN_DIR http://pecl.php.net/get/pecl_http-3.1.0.tgz
	tar -zxf $DOWN_DIR/pecl_http-3.1.0.tgz -C /tmp
	if [ -d /tmp/pecl_http-3.1.0 ];then
		cd /tmp/pecl_http-3.1.0
		$INSTALL_DIR/bin/phpize 
		./configure --with-php-config=$INSTALL_DIR/bin/php-config
		if [ $? -eq 0 ];then
			make && make install
			echo "extension=http.so" >> $INSTALL_DIR/etc/php.ini
		fi
	rm -rf /tmp/pecl_http-3.1.0
	fi
}

# install curl.so
function Install_curl() {
	cd $EXT_DIR/curl
	$INSTALL_DIR/bin/phpize 
	./configure --with-php-config=$INSTALL_DIR/bin/php-config
	if [ $? -eq 0 ];then
		make && make install
		echo "extension=curl.so" >> $INSTALL_DIR/etc/php.ini
	fi
}

# install mcrypt.so
function Install_mcrypt() {
	yum install -y libmcrypt libmcrypt-devel
	cd $EXT_DIR/mcrypt
	$INSTALL_DIR/bin/phpize
	./configure --with-php-config=$INSTALL_DIR/bin/php-config
	if [ $? -eq 0 ];then
		make && make install
		echo "extension=mcrypt.so" >> $INSTALL_DIR/etc/php.ini
	fi
}

# install openssl.so
function Install_openssl() {
	cd $EXT_DIR/openssl
	if [ ! -f config.m4 ];then
		cp config0.m4 config.m4
	fi
	$INSTALL_DIR/bin/phpize
	./configure --with-php-config=$INSTALL_DIR/bin/php-config
	if [ $? -eq 0 ];then
		make && make install
		echo "extension=openssl.so" >> $INSTALL_DIR/etc/php.ini
	fi
}

# install redis.so
function Install_redis() {
	wget -P $DOWN_DIR http://pecl.php.net/get/redis-3.1.4.tgz
	tar -zxf $DOWN_DIR/redis-3.1.4.tgz -C $EXT_DIR/
	cd $EXT_DIR/redis-3.1.4
	$INSTALL_DIR/bin/phpize 
	./configure  --with-php-config=$INSTALL_DIR/bin/php-config
	if [ $? -eq 0 ];then
		make && make install
		echo "extension=redis.so" >> $INSTALL_DIR/etc/php.ini
	fi
	rm -rf $EXT_DIR/redis-3.1.4
} 

# install imagick.so
function Install_Imagick() {
	wget -P $DOWN_DIR http://pecl.php.net/get/imagick-3.4.3.tgz
	tar -zxf $DOWN_DIR/imagick-3.4.3.tgz -C $EXT_DIR/
	cd $EXT_DIR/imagick-3.4.3
	$INSTALL_DIR/bin/phpize
	./configure --with-php-config=$INSTALL_DIR/bin/php-config
	if [ $? -eq 0 ];then
		make && make install
		echo "extension=imagick.so" >> $INSTALL_DIR/etc/php.ini
	fi
	rm -rf $EXT_DIR/imagick-3.4.3
}

# install zip.so
function Install_zip() {
	cd $EXT_DIR/zip
	$INSTALL_DIR/bin/phpize
	./configure --with-php-config=$INSTALL_DIR/bin/php-config
	if [ $? -eq 0 ];then
		make && make install
	fi 
}

# install socket.so
function Install_sock() {
	cd $EXT_DIR/sockets
        $INSTALL_DIR/bin/phpize
        ./configure --with-php-config=$INSTALL_DIR/bin/php-config
        if [ $? -eq 0 ];then
                make && make install
		echo "extension=sockets.so" >> $INSTALL_DIR/etc/php.ini
        fi	
}

# install gettext
function Install_gettext() {
	cd $EXT_DIR/gettext
	$INSTALL_DIR/bin/phpize
	./configure --with-php-config=$INSTALL_DIR/bin/php-config 
	if [ $? -eq 0 ];then
		make && make install
		echo "extension=gettext.so" >> $INSTALL_DIR/etc/php.ini
	fi
}

function Install_bcmath() {
	cd $EXT_DIR/bcmath
	$INSTALL_DIR/bin/phpize
        ./configure --with-php-config=$INSTALL_DIR/bin/php-config 
        if [ $? -eq 0 ];then
                make && make install
                echo "extension=bcmath.so" >> $INSTALL_DIR/etc/php.ini
        fi
	
}

# install exif
function Install_exif() {
	cd $EXT_DIR/exif
	$INSTALL_DIR/bin/phpize
        ./configure --with-php-config=$INSTALL_DIR/bin/php-config 
        if [ $? -eq 0 ];then
                make && make install
                echo "extension=exif.so" >> $INSTALL_DIR/etc/php.ini
        fi
	
}

# install pcntl
function Install_pcntl() {
	cd $EXT_DIR/pcntl
	$INSTALL_DIR/bin/phpize
        ./configure --with-php-config=$INSTALL_DIR/bin/php-config 
        if [ $? -eq 0 ];then
                make && make install
		echo "[messages]" >> $INSTALL_DIR/etc/php.ini
                echo "extension=pcntl.so" >> $INSTALL_DIR/etc/php.ini
        fi
	
}


# install opcache
function Install_opcache() {
	cd $EXT_DIR/opcache
	$INSTALL_DIR/bin/phpize
        ./configure --with-php-config=$INSTALL_DIR/bin/php-config 
        if [ $? -eq 0 ];then
                make && make install
		echo "[zend extensions]" >> $INSTALL_DIR/etc/php.ini
                echo "zend_extension=opcache.so" >> $INSTALL_DIR/etc/php.ini
        fi
}
	

function Install_gd() {
	yum install -y libXpm-devel zlib-devel zlib 
	wget -P $DOWN_DIR http://www.ijg.org/files/jpegsrc.v9b.tar.gz
	tar -zxf $DOWN_DIR/jpegsrc.v9b.tar.gz -C /tmp/
	if [ ! -d /tmp/jpeg-9b];then
		exit
	else
		cd /tmp/jpeg-9b
		./configure --enable-shared --enable-static
		if [ $? -eq 0 ];then
			make && make install
		fi
	rm -rf /tmp/jpeg-9b
	fi
	wget -P $DOWN_DIR/ https://jaist.dl.sourceforge.net/project/libpng/libpng16/1.6.34/libpng-1.6.34.tar.gz
	tar -zxf $DOWN_DIR/libpng-1.6.34.tar.gz -C /tmp/
	if [ ! -d /tmp/libpng-1.6.34 ];then
		exit
	else
		cd /tmp/libpng-1.6.34
		./configure --enable-shared --enable-static
		if [ $? -eq 0 ];then
			make && make install
		fi
		rm -rf /tmp/libpng-1.6.34
	fi
	wget -P $DOWN_DIR https://jaist.dl.sourceforge.net/project/freetype/freetype2/2.8.1/freetype-2.8.1.tar.gz
	tar -zxf $DOWN_DIR/freetype-2.8.1.tar.gz -C /tmp/
	if [ -d  /tmp/freetype-2.8.1 ];then
		cd /tmp/freetype-2.8.1
		./configure --prefix=/usr/lib64
		if [ $? -eq 0 ];then
			make && make install
		fi
		rm -rf /tmp/freetype-2.8.1
	fi
	if [ -d $EXT_DIR/gd ];then
		cd $EXT_DIR/gd
		$INSTALL_DIR/bin/phpize .
		./configure --prefix=/usr/lib64 --with-jpeg=/usr/lib64 --with-png=/usr/lib64 --with-zlib --with-freetype=/usr/lib64 --with-php-config=$INSTALL_DIR/bin/php-config
		if [ $? -eq 0 ];then
			make && make install
		fi
		if [ -f $INSTALL_DIR/etc/php.ini ];then
			echo "extension=gd.so" >> $INSTALL_DIR/etc/php.ini
		fi
	fi
	
	
}

Install_php
if [ $? -eq 0 ];then
	Install_raphf
if [ $? -eq 0 ];then
	Install_propro
if [ $? -eq 0 ];then
	Install_http
if [ $? -eq 0 ];then
	Install_curl
if [ $? -eq 0 ];then
echo ">>>>>> Install mcrypt"
	Install_mcrypt
if [ $? -eq 0 ];then
echo ">>>>>> Install openssl"
	Install_openssl
if [ $? -eq 0 ];then
echo ">>>>>> Install redis"
	Install_redis
if [ $? -eq 0 ];then
echo ">>>>>> Install imagick"
	Install_Imagick
if [ $? -eq 0 ];then
echo ">>>>>> Install zip"
	Install_zip
if [ $? -eq 0 ];then
echo ">>>>>> install sock"
	Install_sock
if [ $? -eq 0 ];then
echo ">>>>>> Install gettext"
	Install_gettext
if [ $? -eq 0 ];then
echo ">>>>>> Install exif"
	Install_exif

# install pcntl
if [ $? -eq 0 ];then
	echo ">>>>>> Install pcntl"
	Install_pcntl

#install opcache
if [ $? -eq 0 ];then
	echo ">>>>>> Install opcache"
	Install_opcache

if [ $? -eq 0 ];then
echo ">>>>>> Install gd"
	Install_gd
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
echo "Install over========================"